package com.linkoog.devtools.code_generator.wizard.generate.database.table;

import com.linkoog.devtools.code_generator.database.dao.DatabaseDao;
import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.model.datasource.ConnectionInfoItem;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.DatasourceInfoContextPanel;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.DbtableInfoContextPanel;
import com.linkoog.devtools.intellij.wizard.WizardContext;
import com.linkoog.devtools.code_generator.wizard.GenerateWizardContext;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.ui.components.JBLoadingPanel;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class DbtableSelectStepPanel implements Disposable {

    private JPanel myMainPanel;


    private DatasourceInfoContextPanel datasourceInfoContextPanel;
    private DbtableInfoContextPanel dbtableInfoContextPanel;

    final JBLoadingPanel loadingPanel = new JBLoadingPanel(new BorderLayout(), this);

    public DbtableSelectStepPanel() {
        loadingPanel.add(myMainPanel, BorderLayout.CENTER);
        loadingPanel.setMinimumSize(JBUI.size(900, 710));
        loadingPanel.setPreferredSize(JBUI.size(950, 750));

        initDatasourcePanel();
        initDbtablePanel();
    }

    @Override
    public void dispose() {
        if (loadingPanel.isLoading()) {
            loadingPanel.stopLoading();
        }
    }


    private void initDatasourcePanel() {
        datasourceInfoContextPanel = new DatasourceInfoContextPanel(loadingPanel);
        datasourceInfoContextPanel.setShowTableConsumer(this::resetTableInfoTable);
        datasourceInfoContextPanel.setResetDatasourceCallback(() -> {
            dbtableInfoContextPanel.resetTableInfoJTable(new ArrayList<>());
        });

        this.myMainPanel.add(datasourceInfoContextPanel.getMyMainPanel(), BorderLayout.NORTH);
    }

    private void initDbtablePanel() {
        dbtableInfoContextPanel = new DbtableInfoContextPanel();
        this.myMainPanel.add(dbtableInfoContextPanel.getMyMainPanel(), BorderLayout.CENTER);
    }

    private void resetTableInfoTable(ConnectionInfoItem datasourceInfoItem) {
        if (datasourceInfoItem == null || datasourceInfoItem.getCurrentDatabase() == null || datasourceInfoItem.getCurrentSchema() == null) {
            dbtableInfoContextPanel.resetTableInfoJTable(new ArrayList<>());
            return;
        }

        DatabaseDao databaseDao = datasourceInfoItem.getDatabaseDao();
        List<TableInfo> tableInfoList = databaseDao.getTableInfoList(datasourceInfoItem.getCurrentDatabase(), datasourceInfoItem.getCurrentSchema());
        dbtableInfoContextPanel.resetTableInfoJTable(tableInfoList);
    }


    public boolean validate() throws ConfigurationException {
        String error = null;

        error = datasourceInfoContextPanel.validate();
        if (error != null) {
            throw new ConfigurationException(error, "错误");
        }

        error = dbtableInfoContextPanel.validate();
        if (error != null) {
            throw new ConfigurationException(error, "错误");
        }

        return true;
    }

    public void finish(@NotNull WizardContext wizardContext) {
        GenerateWizardContext generateWizardContext = (GenerateWizardContext) wizardContext;
        generateWizardContext.setTableInfoList(dbtableInfoContextPanel.getSelectedDBTable());
    }


    public JPanel getMyMainPanel() {
        return loadingPanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }


}
